package com.itheima.health.business;

import com.itheima.health.common.ReportConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
@Slf4j
public class ReportBusiness {

    @Autowired
    private MemberDao memberDao;

    /**
     * 获取截止某月月末的会员总数
     * @param month
     * @return
     */
    @Cacheable(cacheNames = "MemberReport", key = "#month")
    public Integer getCountByMonth(String month) {
        log.info("[统计分析-统计会员数量]month:{}",month);
        // 使用传入的"yyyy.MM"构造LocalDate对象
        LocalDate date = DateTimeUtil.parse(month + ReportConst.SUFFIX_DAY,
                ReportConst.PATTERN_YEAR_MONTH_DAY);
        // 查询截止月末的用户总数
        date = DateTimeUtil.getLastDate(date);
        return memberDao.countBeforeRegTime(date);
    }
}
